<template>
  <el-dialog v-dialogDrag :title="title" :visible.sync="open" class="dialogStyleSmall" :close-on-click-modal="false" :before-close="cancel">
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="字典类型">
        <el-input v-model="form.dictType" :disabled="true" size="medium" />
      </el-form-item>
      <el-form-item label="数据标签" prop="dictLabel">
        <el-input v-model="form.dictLabel" placeholder="请输入数据标签" size="medium" />
      </el-form-item>
      <el-form-item label="数据键值" prop="dictValue">
        <el-input v-model="form.dictValue" placeholder="请输入数据键值" size="medium" />
      </el-form-item>
      <el-form-item label="显示排序" prop="dictSort">
        <el-input-number v-model="form.dictSort" controls-position="right" :min="0" size="medium" />
      </el-form-item>
      <el-form-item label="状态" prop="status" size="medium">
        <label slot="label" v-html="`状&emsp;&emsp;态`" />
        <el-radio-group v-model="form.status">
          <el-radio
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            size="medium"
            :label="dict.dictValue"
          >{{ dict.dictLabel }} </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <label slot="label" v-html="`备&emsp;&emsp;注`" />
        <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button size="medium" @click="cancel">取 消</el-button>
      <el-button type="primary" size="medium" @click="submitForm">保 存</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getData, addData, updateData } from '@/api/system-tool/dict/data'
export default {
  name: 'DataDialog',
  props: {
    statusOptions: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {
        dictCode: undefined,
        dictLabel: '',
        dictValue: '',
        dictSort: 0,
        status: '0',
        remark: ''
      },
      // 表单校验
      rules: {
        dictLabel: [
          { required: true, message: '数据标签不能为空', trigger: 'blur' }
        ],
        dictValue: [
          { required: true, message: '数据键值不能为空', trigger: 'blur' }
        ],
        dictSort: [
          { required: true, message: '数据顺序不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    handleAdd(title, dictType, ids) {
      this.open = true
      this.title = title
      this.form.dictType = dictType
      if (ids) {
        this.getDetailById(ids, dictType)
      }
    },
    getDetailById(dictCode, dictType) {
      getData(dictCode).then(res => {
        const { dictCode, dictLabel, dictValue, dictSort, status, remark } = res.data
        this.form = { dictType, dictCode, dictLabel, dictValue, dictSort, status, remark }
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.form.dictCode = undefined
      this.resetForm('form')
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.dictCode !== undefined) {
            updateData(this.form).then(res => {
              if (res) {
                this.msgSuccess('修改成功')
                this.cancel()
                this.$parent.getList()
              } else {
                this.msgError(res.message)
              }
            })
          } else {
            addData(this.form).then(res => {
              if (res) {
                this.msgSuccess('新增成功')
                this.cancel()
                this.$parent.getList()
              } else {
                this.msgError(res.message)
              }
            })
          }
        }
      })
    }
  }
}
</script>

